package com.wzdigit.wms.raw.task;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.raw.constant.SyncTypeConstant;
import com.wzdigit.wms.raw.domain.dto.IfSappullHeaderDto;
import com.wzdigit.wms.raw.exception.SyncException;
import com.wzdigit.wms.raw.service.mid.IfSappullHeaderService;
import com.wzdigit.wms.raw.service.mid.SyncErrorDataService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName IfSappullToSappullTask
 * @Description 接口中间表-SAP出库单 to 出库单 调拨单
 * @Author LHR-PC
 * @Date 2021/3/16 10:41
 * @Version
 **/
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class IfSappullToGdnOrAsnOrTransferTask {

    @Autowired
    private IfSappullHeaderService ifSappullHeaderService;

    @Autowired
    private SyncErrorDataService syncErrorDataService;

    final String syncType = SyncTypeConstant.IF_PULL_TO_GDN_OR_ASN;

    @Scheduled(cron = "0 0/5 * * * ?")
    public synchronized void syncData() {
        try {
            log.info("if:Sappull => gdn or transfer start...");
            long start = System.currentTimeMillis();
            List<IfSappullHeaderDto> datas = ifSappullHeaderService.selectSyncData();
            if (!CollectionUtil.isEmpty(datas)) {
                syncData(datas, false);
            }
            log.info("------if:Sappull =>gdn or  transfer end:{}------", System.currentTimeMillis() - start);
        } catch (RestStatusException ex) {
            ex.printStackTrace();
        }
    }

    private void syncData(List<IfSappullHeaderDto> datas, boolean isSyncError) {
        for (IfSappullHeaderDto data : datas) {

            if("1".equals(data.getAttribute1())||"3-1".equals(data.getAttribute1())){   //1 出库 3-1
                try {
                    ifSappullHeaderService.syncDataGdn(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "SAP出库单同步出库失败，单号{}", "SAP出库单同步出库失败：", "【SAP出库单同步失败】");
                }
            }else if("2".equals(data.getAttribute1())){// 2 入库
                    try {
                        ifSappullHeaderService.syncDataAsn(data, isSyncError);
                    } catch (Exception e) {
                        exceptionHander(data, e, "SAP出库单同步入库失败，单号{}", "SAP出库单同步入库失败：", "【SAP出库单同步失败】");
                    }
            }else if("2-1".equals(data.getAttribute1())){// 2 易损件退料入库
                try {
                    ifSappullHeaderService.syncDataAsn(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "SAP出库单同步入库失败，单号{}", "SAP出库单同步入库失败：", "【SAP出库单同步失败】");
                }
            }else if("3-2".equals(data.getAttribute1())||"3-3".equals(data.getAttribute1())){//调拨单
                try {
                    ifSappullHeaderService.syncDataTransfer(data, isSyncError);
                } catch (Exception e) {
                    exceptionHander(data, e, "SAP出库单同步调拨失败，单号{}", "SAP出库单同步调拨失败：", "【SAP出库单同步失败】");
                }

            }

        }
    }

    public void exceptionHander(IfSappullHeaderDto data, Exception e, String s, String s2, String s3) {
        log.warn(s, data.getRsnum());
        log.warn(s2 + e.getMessage() == null ? e.toString() : e.getMessage());
        String message = "";
        if (SyncException.class.isInstance(e)) {
            SyncException ex = (SyncException) e;
            message = ex.getFailedReason();
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            log.info(s3 + ex.getFailedReason());
            syncErrorDataService.saveSyncErrorData(ex.getSyncErrorType(), ex.getCode(), message);
        } else {
            e.printStackTrace();
            if (e.getMessage() == null) {
                message = e.getClass().getName();
            } else {
                message = e.getMessage();
            }
            if (message.length() >= 1000) {
                message = message.substring(0, 500);
            }
            syncErrorDataService.saveSyncErrorData(syncType, data.getRsnum(), message);
        }
        e.printStackTrace();
    }
}
